//
//  ApiTaskFactory.m
//  ApiBase
//
//  Created by Liam on 12-10-3.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "ApiTaskFactory.h"

@implementation ApiTaskFactory

static NSMutableDictionary* g_registry = nil;

+ (void)initialRegistry
{
	if (!g_registry)
	{
		g_registry = [[NSMutableDictionary dictionary] retain];
	}
}

+ (id)regClientTask:(NSInteger)type taskClass:(Class)taskClass
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	[ApiTaskFactory initialRegistry];
	
	NSNumber* key = [NSNumber numberWithInt:type];
	[g_registry setObject:NSStringFromClass(taskClass) forKey:key];
	
	[pool release];

	return nil;
}

+ (ApiTaskBase*) getApiTask:(NSInteger)type params:(NSDictionary*)params context:(id)context delegate:(id)delegate
{
	ApiTaskBase* task = nil;
	
	NSNumber* key = [NSNumber numberWithInt:type];
	NSString* className = [g_registry objectForKey:key];
	
	if (className && NSClassFromString(className))
	{
		task = [[[NSClassFromString(className) alloc] init] autorelease];
		task.params = params;
		task.delegate = delegate;
		task.taskType = type;
		task.context = context;
	}
	return task;	
}

@end
